package com.xw.bo.ks.entity.base;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author： XW -> ~
 * @create： 2023/12/8 11:16
 */
public class WrapperBuild {

    private Byte isDelete;

    private WrapperBuild wrapperBuild;

    private Map<String, Object> wrapperMap;

    public static WrapperBuild create() {
        return create(null, null);
    }

    public static WrapperBuild create(Byte isDelete) {
        return create(null);
    }

    public static WrapperBuild create(Byte isDelete, Integer mapType) {
        WrapperBuild build = new WrapperBuild();

        if (isDelete == null || isDelete == 1) {
            build.isDelete = 1;
        } else {
            build.isDelete = isDelete;
        }

        if (mapType == null || mapType == 1) {
            build.wrapperMap = new HashMap<>();
        } else {
            build.wrapperMap = new ConcurrentHashMap<>();
        }
        return build;
    }

    /**
     * 默认参数是空的，就不添加了
     * @param key 键
     * @param value 值
     * @return build对象
     */
    public WrapperBuild put(String key, Object value) {
        if (StringUtils.isEmpty(key) || value == null){
            return this;
        }
        wrapperMap.put(key, value);
        return this;
    }

    public WrapperBuild putNull(String key, Object value) {
        wrapperMap.put(key, value);
        return this;
    }

    public QueryWrapper buildWrapper() {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        wrapperMap.forEach(queryWrapper::eq);
        if (isDelete != null) {
            queryWrapper.eq("is_delete", isDelete);
        }
        return queryWrapper;
    }

}
